package com.monna.games_2048;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.monna.util.UserUtil;

/**
 * 用户登陆、注册Activity
 * @author 廖梦青
 */
public class LoginActivity extends AppCompatActivity {

    private EditText account;
    private EditText password;
    private String userAccount;
    private String userPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        SharedPreferences preferences = getSharedPreferences("userInfo", this.MODE_PRIVATE);
        String account = preferences.getString("account", "");
        if (account.length() == 0){
            Button login = findViewById(R.id.bt_login);
            Button resiger = findViewById(R.id.bt_register);
            /**
             * 登陆按钮监听事件
             */
            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isLogin()){
                        saveLoginStatus();
                        Intent intent = new Intent(LoginActivity.this, IndexActivity.class);
                        finish();
                        startActivity(intent);
                    }else {
                        alertMessage();
                    }
                }
            });
            /**
             * 注册按钮监听事件,跳转至注册界面
             */
            resiger.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                    startActivity(intent);
                }
            });
        }else {
            Intent intent = new Intent(LoginActivity.this, IndexActivity.class);
            finish();
            startActivity(intent);
        }
    }

    /**
     * 判断用户是否登陆成功
     * @return
     */
    private boolean isLogin(){
        account = findViewById(R.id.val_account);
        password = findViewById(R.id.val_password);
        userAccount = account.getText().toString().trim();
        userPassword = password.getText().toString().trim();
        if (new UserUtil(this).userLogin(userAccount, userPassword)){
            return true;
        }
        return false;
    }

    /**
     * 弹出对话框提示用户登陆失败
     */
    private void alertMessage(){
        new AlertDialog.Builder(this)
                .setTitle("登陆失败")
                .setIcon(R.mipmap.ic_start2048)
                .setMessage("账号或密码错误！")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = getIntent();
                finish();
                startActivity(intent);
            }
        }).show();
    }

    /**
     * 存储用户登陆信息
     */
    public void saveLoginStatus(){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("account", userAccount);
        editor.putString("password", userPassword);
        editor.commit();
    }
}
